#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "k_connector_comm.h"
#include "k_connector_ioctl.h"
#include "k_vo_comm.h"
#include "mpi_connector_api.h"

#define pr_debug(...) // printf(__VA_ARGS__)
#define pr_info(...)  // printf(__VA_ARGS__)
#define pr_warn(...)  // printf(__VA_ARGS__)
#define pr_err(...)   printf(__VA_ARGS__)

k_connector_info connector_info_list[] = {
#if defined(CONFIG_MPP_ENABLE_DSI_DEBUGGER)
    {
        .connector_name = "debugger",
        .type = DSI_DEBUGGER_DEVICE,
    },
#endif
    {
        "virtdev",
        0,
        0,
        BACKGROUND_BLACK_COLOR,
        7,
        0,
        1,
        K_DSI_4LAN,
        K_BURST_MODE,
        K_VO_LP_MODE,
        { 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        VIRTUAL_DISPLAY_DEVICE,
    },

#if defined(CONFIG_MPP_DSI_ENABLE_HDMI_LT9611)
    {
        "lt9611",
        0,
        0,
        BACKGROUND_BLACK_COLOR,
        10,
        0,
        1,
        K_DSI_4LAN,
        K_BURST_MODE,
        K_VO_LP_MODE,
        { 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        LT9611_MIPI_ADAPT_RESOLUTION,
    },
    {
        "lt9611",
        0,
        0,
        BACKGROUND_BLACK_COLOR,
        10,
        3,
        1,
        K_DSI_4LAN,
        K_BURST_MODE,
        K_VO_LP_MODE,
        { 15, 295, 0x09, 0x96 },
        { 148500, 891000, 2200, 1920, 44, 148, 88, 1125, 1080, 5, 4, 36 },
        LT9611_MIPI_4LAN_1920X1080_60FPS,
    },
    {
        "lt9611",
        0,
        0,
        BACKGROUND_BLACK_COLOR,
        10,
        3,
        1,
        K_DSI_4LAN,
        K_BURST_MODE,
        K_VO_LP_MODE,
        { 15, 295, 0x09, 0x96 },
        { 148500, 891000, 2200, 1920, 44, 148, 88, 1125, 1080, 5, 4, 36 },
        LT9611_MIPI_4LAN_1920X1080_30FPS,
    },
    {
        "lt9611",
        0,
        0,
        BACKGROUND_BLACK_COLOR,
        9,
        7,
        1,
        K_DSI_4LAN,
        K_BURST_MODE,
        K_VO_LP_MODE,
        { 15, 295, 0x19, 0x96 },
        { 74250, 445500, 1616, 1280, 40, 220, 76, 765, 720, 5, 4, 36 },
        LT9611_MIPI_4LAN_1280X720_60FPS,
    },
    {
        "lt9611",
        0,
        0,
        BACKGROUND_BLACK_COLOR,
        9,
        7,
        1,
        K_DSI_4LAN,
        K_BURST_MODE,
        K_VO_LP_MODE,
        { 15, 295, 0x19, 0x96 },
        { 74250, 445500, 1940, 1280, 40, 220, 400, 765, 720, 5, 4, 36 },
        LT9611_MIPI_4LAN_1280X720_50FPS,
    },
    {
        "lt9611",
        0,
        0,
        BACKGROUND_BLACK_COLOR,
        9,
        7,
        1,
        K_DSI_4LAN,
        K_BURST_MODE,
        K_VO_LP_MODE,
        { 15, 295, 0x19, 0x96 },
        { 74250, 445500, 3232, 1280, 40, 220, 1692, 765, 720, 5, 4, 36 },
        LT9611_MIPI_4LAN_1280X720_30FPS,
    },
    {
        "lt9611",
        0,
        0,
        BACKGROUND_BLACK_COLOR,
        9,
        23,
        1,
        K_DSI_4LAN,
        K_BURST_MODE,
        K_VO_LP_MODE,
        { 15, 394, 0x39, 0x96 },
        { 24750, 148500, 800, 640, 96, 48, 16, 525, 480, 5, 4, 36 },
        LT9611_MIPI_4LAN_640X480_60FPS,
    },
#endif

#if defined(CONFIG_MPP_DSI_ENABLE_LCD_HX8399)
    {
        // TEST PASS
        // this configure is generated by tools.
        .connector_name = "hx8399", // maybe should change it
        .screen_test_mode = 0,
        .dsi_test_mode = 0,
        .bg_color = BACKGROUND_BLACK_COLOR,
        .intr_line = 11, // 2048 lines
        .pixclk_div = 3,
        .lan_num = K_DSI_4LAN,
        .work_mode = K_BURST_MODE,
        .cmd_mode = K_VO_LP_MODE,
        .phy_attr = {
            .n = 15,
            .m = 295,
            .voc = 0x07, // 0b00000111
            .hs_freq = 0x80 | 0x3a, // 0b10111010
        },
        .resolution = {
            .pclk = 148500, // 148500 * 1000 / (1080 + 20 + 20 + 120) / (1920 + 50 + 52 + 124) = 55.81 fps
            .phyclk = 891000,
            .htotal = (1080 + 20 + 20 + 120), // 1240
            .hdisplay = 1080,
            .hsync_len = 20,
            .hback_porch = 20,
            .hfront_porch = 120,
            .vtotal = (1920 + 50 + 52 + 124), // 2146
            .vdisplay = 1920,
            .vsync_len = 50,
            .vback_porch = 52,
            .vfront_porch = 124,
        },
        .type = HX8377_V2_MIPI_4LAN_1080X1920_30FPS, // maybe should change it
    },
#endif

#if defined(CONFIG_MPP_DSI_ENABLE_LCD_ST7701)
    {
        // this configure is generated by tools.
        .connector_name = "st7701", // maybe should change it
        .screen_test_mode = 0,
        .dsi_test_mode = 0,
        .bg_color = BACKGROUND_BLACK_COLOR,
        .intr_line = 10, // 1024 lines
        .pixclk_div = 11,
        .lan_num = K_DSI_2LAN,
        .work_mode = K_BURST_MODE,
        .cmd_mode = K_VO_LP_MODE,
        .phy_attr = {
            .n = 3,
            .m = 97,
            .voc = 0x17, // 0b00010111
            .hs_freq = 0x80 | 0x07, // 0b10000111
        },
        .resolution = {
            .pclk = 49500, // 49500 * 1000 / (480 + 10 + 20 + 150) / (854 + 10 + 20 + 316) = 62.50 fps
            .phyclk = 594000,
            .htotal = (480 + 10 + 20 + 150), // 660
            .hdisplay = 480,
            .hsync_len = 10,
            .hback_porch = 20,
            .hfront_porch = 150,
            .vtotal = (854 + 10 + 20 + 316), // 1200
            .vdisplay = 854,
            .vsync_len = 10,
            .vback_porch = 20,
            .vfront_porch = 316,
        },
        .type = ST7701_V1_MIPI_2LAN_480X854_30FPS, // maybe should change it
    },

#if defined(CONFIG_BOARD_K230_CANMV_LCKFB)
    {
        // TEST PASS
        // this configure is generated by tools.
        .connector_name = "st7701", // maybe should change it
        .screen_test_mode = 0,
        .dsi_test_mode = 0,
        .bg_color = BACKGROUND_BLACK_COLOR,
        .intr_line = 10, // 1024 lines
        .pixclk_div = 23,
        .lan_num = K_DSI_2LAN,
        .work_mode = K_BURST_MODE,
        .cmd_mode = K_VO_LP_MODE,
        .phy_attr = {
            .n = 3,
            .m = 97,
            .voc = 0x27, // 0b00100111
            .hs_freq = 0x80 | 0x25, // 0b10100101
        },
        .resolution = {
            .pclk = 24750, // 24750 * 1000 / (480 + 8 + 32 + 32) / (800 + 10 + 150 + 140) = 40.76 fps
            .phyclk = 297000,
            .htotal = (480 + 8 + 32 + 32), // 552
            .hdisplay = 480,
            .hsync_len = 8,
            .hback_porch = 32,
            .hfront_porch = 32,
            .vtotal = (800 + 10 + 150 + 140), // 1100
            .vdisplay = 800,
            .vsync_len = 10,
            .vback_porch = 150,
            .vfront_porch = 140,
        },
        .type = ST7701_V1_MIPI_2LAN_480X800_30FPS, // maybe should change it
    },
#else
    {
        // TEST PASS
        // this configure is generated by tools.
        .connector_name = "st7701", // maybe should change it
        .screen_test_mode = 0,
        .dsi_test_mode = 0,
        .bg_color = BACKGROUND_BLACK_COLOR,
        .intr_line = 10, // 1024 lines
        .pixclk_div = 15,
        .lan_num = K_DSI_2LAN,
        .work_mode = K_BURST_MODE,
        .cmd_mode = K_VO_LP_MODE,
        .phy_attr = {
            .n = 15,
            .m = 295,
            .voc = 0x17, // 0b00010111
            .hs_freq = 0x80 | 0x16, // 0b10010110
        },
        .resolution = {
            .pclk = 37125, // 37125 * 1000 / (480 + 2 + 16 + 50) / (800 + 60 + 180 + 60) = 61.59 fps
            .phyclk = 445500,
            .htotal = (480 + 2 + 16 + 50), // 548
            .hdisplay = 480,
            .hsync_len = 2,
            .hback_porch = 16,
            .hfront_porch = 50,
            .vtotal = (800 + 60 + 180 + 60), // 1100
            .vdisplay = 800,
            .vsync_len = 60,
            .vback_porch = 180,
            .vfront_porch = 60,
        },
        .type = ST7701_V1_MIPI_2LAN_480X800_30FPS, // maybe should change it
    },
#endif

#if defined(CONFIG_BOARD_K230D_CANMV_ATK_DNK230D)
    {
        // TEST PASS
        // this configure is generated by tools.
        .connector_name = "st7701", // maybe should change it
        .screen_test_mode = 0,
        .dsi_test_mode = 0,
        .bg_color = BACKGROUND_BLACK_COLOR,
        .intr_line = 10, // 1024 lines
        .pixclk_div = 14,
        .lan_num = K_DSI_2LAN,
        .work_mode = K_BURST_MODE,
        .cmd_mode = K_VO_LP_MODE,
        .phy_attr = {
            .n = 4,
            .m = 97,
            .voc = 0x17, // 0b00010111
            .hs_freq = 0x80 | 0x26, // 0b10100110
        },
        .resolution = {
            .pclk = 39600, // 39600 * 1000 / (480 + 20 + 20 + 80) / (640 + 10 + 50 + 400) = 60.00 fps
            .phyclk = 475200,
            .htotal = (480 + 20 + 20 + 80), // 600
            .hdisplay = 480,
            .hsync_len = 20,
            .hback_porch = 20,
            .hfront_porch = 80,
            .vtotal = (640 + 10 + 50 + 400), // 1100
            .vdisplay = 640,
            .vsync_len = 10,
            .vback_porch = 50,
            .vfront_porch = 400,
        },
        .type = ST7701_V1_MIPI_2LAN_480X640_30FPS, // maybe should change it
    },
#else
    {
        // TEST PASS
        // this configure is generated by tools.
        .connector_name = "st7701", // maybe should change it
        .screen_test_mode = 0,
        .dsi_test_mode = 0,
        .bg_color = BACKGROUND_BLACK_COLOR,
        .intr_line = 10, // 1024 lines
        .pixclk_div = 17,
        .lan_num = K_DSI_2LAN,
        .work_mode = K_BURST_MODE,
        .cmd_mode = K_VO_LP_MODE,
        .phy_attr = {
            .n = 3,
            .m = 64,
            .voc = 0x1f, // 0b00011111
            .hs_freq = 0x80 | 0x05, // 0b10000101
        },
        .resolution = {
            .pclk = 33000, // 33000 * 1000 / (480 + 6 + 10 + 20) / (640 + 60 + 180 + 220) = 58.14 fps
            .phyclk = 396000,
            .htotal = (480 + 6 + 10 + 20), // 516
            .hdisplay = 480,
            .hsync_len = 6,
            .hback_porch = 10,
            .hfront_porch = 20,
            .vtotal = (640 + 60 + 180 + 220), // 1100
            .vdisplay = 640,
            .vsync_len = 60,
            .vback_porch = 180,
            .vfront_porch = 220,
        },
        .type = ST7701_V1_MIPI_2LAN_480X640_30FPS, // maybe should change it
    },
#endif

    {
        // TEST PASS
        // this configure is generated by tools.
        .connector_name = "st7701", // maybe should change it
        .screen_test_mode = 0,
        .dsi_test_mode = 0,
        .bg_color = BACKGROUND_BLACK_COLOR,
        .intr_line = 10, // 1024 lines
        .pixclk_div = 21,
        .lan_num = K_DSI_2LAN,
        .work_mode = K_BURST_MODE,
        .cmd_mode = K_VO_LP_MODE,
        .phy_attr = {
            .n = 3,
            .m = 52,
            .voc = 0x1f, // 0b00011111
            .hs_freq = 0x80 | 0x35, // 0b10110101
        },
        .resolution = {
            .pclk = 27000, // 27000 * 1000 / (368 + 8 + 16 + 16) / (552 + 48 + 250 + 250) = 60.16 fps
            .phyclk = 324000,
            .htotal = (368 + 8 + 16 + 16), // 408
            .hdisplay = 368,
            .hsync_len = 8,
            .hback_porch = 16,
            .hfront_porch = 16,
            .vtotal = (552 + 48 + 250 + 250), // 1100
            .vdisplay = 552,
            .vsync_len = 48,
            .vback_porch = 250,
            .vfront_porch = 250,
        },
        .type = ST7701_V1_MIPI_2LAN_368X544_60FPS, // maybe should change it
    },
#endif

#if defined(CONFIG_MPP_DSI_ENABLE_LCD_ILI9806)
    {
        // TEST PASS
        // this configure is generated by tools.
        .connector_name = "ili9806", // maybe should change it
        .screen_test_mode = 0,
        .dsi_test_mode = 0,
        .bg_color = BACKGROUND_BLACK_COLOR,
        .intr_line = 10, // 1024 lines
        .pixclk_div = 15,
        .lan_num = K_DSI_2LAN,
        .work_mode = K_BURST_MODE,
        .cmd_mode = K_VO_LP_MODE,
        .phy_attr = {
            .n = 15,
            .m = 295,
            .voc = 0x17, // 0b00010111
            .hs_freq = 0x80 | 0x16, // 0b10010110
        },
        .resolution = {
            .pclk = 37125, // 37125 * 1000 / (480 + 20 + 20 + 40) / (800 + 50 + 120 + 127) = 60.43 fps
            .phyclk = 445500,
            .htotal = (480 + 20 + 20 + 40), // 560
            .hdisplay = 480,
            .hsync_len = 20,
            .hback_porch = 20,
            .hfront_porch = 40,
            .vtotal = (800 + 50 + 120 + 127), // 1097
            .vdisplay = 800,
            .vsync_len = 50,
            .vback_porch = 120,
            .vfront_porch = 127,
        },
        .type = ILI9806_MIPI_2LAN_480X800_30FPS, // maybe should change it
    },
#endif

#if defined(CONFIG_MPP_DSI_ENABLE_LCD_ILI9881)
    {
        // TEST PASS
        // this configure is generated by tools.
        .connector_name = "ili9881c", // maybe should change it
        .screen_test_mode = 0,
        .dsi_test_mode = 0,
        .bg_color = BACKGROUND_BLACK_COLOR,
        .intr_line = 10, // 1024 lines
        .pixclk_div = 7,
        .lan_num = K_DSI_4LAN,
        .work_mode = K_BURST_MODE,
        .cmd_mode = K_VO_LP_MODE,
        .phy_attr = {
            .n = 15,
            .m = 295,
            .voc = 0x17, // 0b00010111
            .hs_freq = 0x80 | 0x16, // 0b10010110
        },
        .resolution = {
            .pclk = 74250, // 74250 * 1000 / (800 + 8 + 48 + 52) / (1280 + 6 + 16 + 100) = 58.33 fps
            .phyclk = 445500,
            .htotal = (800 + 8 + 48 + 52), // 908
            .hdisplay = 800,
            .hsync_len = 8,
            .hback_porch = 48,
            .hfront_porch = 52,
            .vtotal = (1280 + 6 + 16 + 100), // 1402
            .vdisplay = 1280,
            .vsync_len = 6,
            .vback_porch = 16,
            .vfront_porch = 100,
        },
        .type = ILI9881_MIPI_4LAN_800X1280_60FPS, // maybe should change it
    },
#endif

#if defined(CONFIG_MPP_DSI_ENABLE_LCD_NT35516)
    {
        // TEST PASS
        // this configure is generated by tools.
        .connector_name = "nt35516", // maybe should change it
        .screen_test_mode = 0,
        .dsi_test_mode = 0,
        .bg_color = BACKGROUND_BLACK_COLOR,
        .intr_line = 10, // 1024 lines
        .pixclk_div = 17,
        .lan_num = K_DSI_2LAN,
        .work_mode = K_BURST_MODE,
        .cmd_mode = K_VO_LP_MODE,
        .phy_attr = {
            .n = 3,
            .m = 64,
            .voc = 0x1f, // 0b00011111
            .hs_freq = 0x80 | 0x05, // 0b10000101
        },
        .resolution = {
            .pclk = 33000, // 33000 * 1000 / (536 + 20 + 20 + 40) / (960 + 10 + 20 + 110) = 48.70 fps
            .phyclk = 396000,
            .htotal = (536 + 20 + 20 + 40), // 616
            .hdisplay = 536,
            .hsync_len = 20,
            .hback_porch = 20,
            .hfront_porch = 40,
            .vtotal = (960 + 10 + 20 + 110), // 1100
            .vdisplay = 960,
            .vsync_len = 10,
            .vback_porch = 20,
            .vfront_porch = 110,
        },
        .type = NT35516_MIPI_2LAN_536X960_30FPS, // maybe should change it
    },
#endif

#if defined(CONFIG_MPP_DSI_ENABLE_LCD_NT35532)
    {
        // TEST PASS
        // this configure is generated by tools.
        .connector_name = "nt35532", // maybe should change it
        .screen_test_mode = 0,
        .dsi_test_mode = 0,
        .bg_color = BACKGROUND_BLACK_COLOR,
        .intr_line = 11, // 2048 lines
        .pixclk_div = 3,
        .lan_num = K_DSI_4LAN,
        .work_mode = K_BURST_MODE,
        .cmd_mode = K_VO_LP_MODE,
        .phy_attr = {
            .n = 15,
            .m = 295,
            .voc = 0x07, // 0b00000111
            .hs_freq = 0x80 | 0x3a, // 0b10111010
        },
        .resolution = {
            .pclk = 148500, // 148500 * 1000 / (1080 + 20 + 20 + 120) / (1920 + 50 + 52 + 124) = 55.81 fps
            .phyclk = 891000,
            .htotal = (1080 + 20 + 20 + 120), // 1240
            .hdisplay = 1080,
            .hsync_len = 20,
            .hback_porch = 20,
            .hfront_porch = 120,
            .vtotal = (1920 + 50 + 52 + 124), // 2146
            .vdisplay = 1920,
            .vsync_len = 50,
            .vback_porch = 52,
            .vfront_porch = 124,
        },
        .type = NT35532_MIPI_2LAN_1080X1920_30FPS, // maybe should change it
    },
#endif

#if defined(CONFIG_MPP_DSI_ENABLE_LCD_GC9503)
    {
        // this configure is generated by tools.
        .connector_name = "gc9503", // maybe should change it
        .screen_test_mode = 0,
        .dsi_test_mode = 0,
        .bg_color = BACKGROUND_BLACK_COLOR,
        .intr_line = 10, // 1024 lines
        .pixclk_div = 14,
        .lan_num = K_DSI_2LAN,
        .work_mode = K_BURST_MODE,
        .cmd_mode = K_VO_LP_MODE,
        .phy_attr = {
            .n = 4,
            .m = 97,
            .voc = 0x17, // 0b010111
            .hs_freq = 0x80 | 0x26,
        },
        .resolution = {
            .pclk = 39600, // 39600 * 1000 / (480 + 20 + 20 + 80) / (800 + 10 + 70 + 220) = 60.00 fps
            .phyclk = 475200,
            .htotal = (480 + 20 + 20 + 80), // 600
            .hdisplay = 480,
            .hsync_len = 20,
            .hback_porch = 20,
            .hfront_porch = 80,
            .vtotal = (800 + 10 + 70 + 220), // 1100
            .vdisplay = 800,
            .vsync_len = 10,
            .vback_porch = 70,
            .vfront_porch = 220,
        },
        .type = GC9503_MIPI_2LAN_480X800_60FPS, // maybe should change it
    },
#endif
    {
        .connector_name = NULL,
    }
};

k_s32 kd_mpi_get_connector_info(k_connector_type connector_type, k_connector_info* connector_info)
{
    if (!connector_info) {
        pr_err("%s, connector_info is null\n", __func__);
        return K_ERR_VO_NULL_PTR;
    }

#if defined(CONFIG_MPP_ENABLE_DSI_DEBUGGER)
    if (DSI_DEBUGGER_DEVICE == connector_type) {
        kd_mpi_connector_parse_setting(connector_info_list);
    }
#endif // CONFIG_MPP_ENABLE_DSI_DEBUGGER

    for (k_s32 i = 0; i < sizeof(connector_info_list) / sizeof(k_connector_info); i++) {
        if (connector_type == connector_info_list[i].type) {
            memcpy(connector_info, &connector_info_list[i], sizeof(k_connector_info));
            return 0;
        }
    }

    return K_ERR_UNEXIST;
}

k_s32 kd_mpi_connector_adapt_resolution(k_connector_type type, k_connector_negotiated_data* negotiated_data)
{
    k_u32            ret = 0;
    k_s32            connector_fd;
    k_connector_type connector_type;
    k_connector_info connector_info;

    // step 1
    // read HDMI monitor EDID and negotiate the resolution
    memset(&connector_info, 0, sizeof(k_connector_info));
    connector_type = type;
    ret            = kd_mpi_get_connector_info(connector_type, &connector_info);
    if (ret) {
        printf("%s get connector info failed for connector_type %d\n", __func__, connector_type);
        return ret;
    }

    connector_fd = kd_mpi_connector_open(connector_info.connector_name);
    if (connector_fd < 0) {
        printf("%s connector open %s failed \n", __func__, connector_info.connector_name);
        return K_ERR_VO_NOTREADY;
    }
    kd_mpi_connector_get_negotiated_data(connector_fd, negotiated_data);
    kd_mpi_connector_close(connector_fd);

    // step 2
    // according to the negotiated resolution and then set the prefered resolution
    memset(&connector_info, 0, sizeof(k_connector_info));
    connector_type = negotiated_data->negotiated_types[0];
    ret            = kd_mpi_get_connector_info(connector_type, &connector_info);
    if (ret) {
        printf("%s get connector info failed for connector_type %d\n", __func__, connector_type);
        return ret;
    }

    connector_fd = kd_mpi_connector_open(connector_info.connector_name);
    if (connector_fd < 0) {
        printf("%s connector open %s failed \n", __func__, connector_info.connector_name);
        return K_ERR_VO_NOTREADY;
    }
    kd_mpi_connector_power_set(connector_fd, 1);
    kd_mpi_connector_init(connector_fd, connector_info);

    return 0;
}

k_s32 kd_mpi_connector_open(const char* connector_name)
{
    k_s32 fd = 0;
    char  dev_name[52];

    if (!connector_name) {
        pr_err("%s, sensor_name is null\n", __func__);
        return K_ERR_VO_NULL_PTR;
    }

    snprintf(dev_name, sizeof(dev_name), "/dev/connector_%s", connector_name);
    fd = open(dev_name, O_RDWR);
    if (fd < 0) {
        pr_err("%s, failed(%d).\n", __func__, fd);
        return K_ERR_VO_NOTREADY;
    }
    return fd;
}

k_s32 kd_mpi_connector_close(k_s32 fd)
{
    close(fd);

    return 0;
}

k_s32 kd_mpi_connector_power_set(k_s32 fd, k_bool on)
{
    k_s32 ret;

    ret = ioctl(fd, KD_IOC_CONNECTOR_S_POWER, &on);
    if (ret != 0) {
        pr_err("%s, error(%d)\n", __func__, ret);
        return K_ERR_VO_NOT_SUPPORT;
    }

    return ret;
}

k_s32 kd_mpi_connector_id_get(k_s32 fd, k_u32* sensor_id)
{
    k_s32 ret;

    if (!sensor_id) {
        pr_err("%s, sensor_id is null\n", __func__);
        return K_ERR_VO_NULL_PTR;
    }

    ret = ioctl(fd, KD_IOC_CONNECTOR_G_ID, sensor_id);
    if (ret != 0) {
        pr_err("%s, error(%d)\n", __func__, ret);
        return K_ERR_VO_NOT_SUPPORT;
    }

    return ret;
}

k_s32 kd_mpi_connector_init(k_s32 fd, k_connector_info info)
{
    k_s32 ret;

    ret = ioctl(fd, KD_IOC_CONNECTOR_S_INIT, info);
    if (ret != 0) {
        pr_err("%s, error(%d)\n", __func__, ret);
        return K_ERR_VO_NOT_SUPPORT;
    }

    return ret;
}

k_s32 kd_mpi_connector_get_negotiated_data(k_s32 fd, k_connector_negotiated_data* negotiated_data)
{
    k_s32 ret;

    ret = ioctl(fd, KD_IOC_CONNECTOR_G_NEG_DATA, negotiated_data);
    if (ret == -1) {
        pr_err("%s, error(%d)\n", __func__, ret);
        return K_ERR_VO_NOT_SUPPORT;
    }

    return ret;
}

k_s32 kd_mpi_connector_set_mirror(k_s32 fd, k_connector_mirror mirror)
{
    k_s32 ret;

    ret = ioctl(fd, KD_IOC_CONNECTOR_S_MIRROR, &mirror);
    if (ret != 0) {
        pr_err("%s, error(%d)\n", __func__, ret);
        return K_ERR_VO_NOT_SUPPORT;
    }

    return ret;
}
